﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;

public class TextureCompose  {
    public enum Channel
    {
        R,
        G,
        B,
        A
    }


    private int width,height;

    private Texture2D composeTexture;

    private Texture2D sourceTexture;

    private Dictionary<Channel, Texture2D> channelTexDic = new Dictionary<Channel, Texture2D>();


    public TextureCompose()
    {

    }

    /// <summary>
    /// 单独设置原本图的通道
    /// </summary>
    /// <param name="sourceTexture"></param>
    public TextureCompose(Texture2D sourceTexture)
    {
        this.sourceTexture = sourceTexture;
    }

    /// <summary>
    /// SetTeture
    /// </summary>
    /// <param name="channel"></param>
    /// <param name="channelTex"></param>
    public void SetTexture(Channel channel,Texture2D channelTex)
    {
        if(channelTex == null)
            return;

        var path = AssetDatabase.GetAssetPath(channelTex);
        TextureImporter TextureI = AssetImporter.GetAtPath(path) as TextureImporter;
        TextureI.isReadable = true;
        AssetDatabase.Refresh();
        AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
       channelTexDic[channel] = channelTex;
        if (width == 0 || height == 0)
        {
            width = channelTex.width;
            height = channelTex.height;
        }
        else
        {
            if (width != channelTex.width || height != channelTex.height)
            {
                Debug.LogError("设置的Tex 大小不正确");
            }
        }
    }

    public Color[] GetChannelColors(Channel channel)
    {
        if (channelTexDic.ContainsKey(channel))
            return channelTexDic[channel].GetPixels();


        var defaultColor = new Color[width * height];
        for (int i = 0; i < defaultColor.Length; i++)
        {
            defaultColor[i] = Color.white;
        }

        return defaultColor;
    }


    public void ComposeTexture()
    {
        if (sourceTexture != null)
        {
            composeTexture = sourceTexture;
        }
        else
        {
            composeTexture = new Texture2D(width, height, TextureFormat.ARGB32, false);
        }
        var colorR = GetChannelColors(Channel.R);
        var colorG = GetChannelColors(Channel.G);
        var colorB = GetChannelColors(Channel.B);
        var colorA = GetChannelColors(Channel.A);

        var color = new Color[width * height];

        for (int i = 0; i < color.Length; i++)
        {
            color[i] = new Color(colorR[i].r, colorG[i].r, colorB[i].r, colorA[i].r);
        }

        if(color.Length <= 0)
        {
            throw new Exception("没有贴图宽度");
        }
        composeTexture.SetPixels(color);
        composeTexture.Apply();
    }

    /// <summary>
    /// 保存合并后的贴图
    /// </summary>
    /// <param name="path"></param>
    public void SaveTexture(string path)
    {
        if (composeTexture == null)
        {
            Debug.LogError("贴图还没合并");
            return;
        }
   
        byte[] data = composeTexture.EncodeToPNG();
        File.WriteAllBytes(path,data);
        AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);


        //新贴图需要更新
        if (sourceTexture == null)
        {
            TextureImporter TextureI = AssetImporter.GetAtPath(path) as TextureImporter;
            TextureI.textureCompression = TextureImporterCompression.Compressed;
            TextureI.isReadable = true;
            TextureI.mipmapEnabled = false;
            TextureI.wrapMode = TextureWrapMode.Clamp;
        }

        AssetDatabase.Refresh();

        AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
    }
}
